package com.yuanqi.dz.dc.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import pnbclient.helper.StringHelper;
import pnbclient.util.PnbLogFactory;
import pnblog.PnbLogging;

import com.yuanqi.dz.dc.util.DZConfigRead;
import com.yuanqi.dz.dc.util.FtpUtil;
import com.yuanqi.dz.dc.util.ReadLocalDZFile;
import com.yuanqitech.advancedpower.powerdate.DateUtil;
import com.yuanqitech.advancedpower.powerdate.PowerDateFormat;
import com.yuanqitech.powermop.dz.DZDataInterface;
import com.yuanqitech.powermop.dz.DZInfo;
import com.yuanqitech.powermop.dz.DZInfoArray;

public class DZSerivceImpl  implements DZDataInterface{
	public static final PnbLogging LOG = PnbLogFactory.getLogger("DZ_EXT_LOG");

	public DZInfoArray getDZData(String mercharnum, String dzDate) {
		DZInfoArray  dzInfoArray=new DZInfoArray();
		String  is_private=DZConfigRead.getProperties("is_private");
		String source_type=null;
		String  address=null;
		String local_path =null;
		String fileName=null;
		String dz_date_format=null;
		String dz_date_addday=null;
		String ftp_port =null;
		String  username=null;
		String password = null;
		String  remotePath=null ;
		if(null!=is_private && "0".equals(is_private)){
			String  is_private_mercharnum=DZConfigRead.getProperties("is_private_"+mercharnum);
			if(null!=is_private_mercharnum && is_private_mercharnum.equals("1")){
				source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
				address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
				local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
				fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
				dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
				dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
				ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
				username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
				password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
				remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
			}else{
				source_type=DZConfigRead.getProperties("source_type");
				address=DZConfigRead.getProperties("ftp_ip");
				local_path=DZConfigRead.getProperties("local_path");//本地目录
				fileName=DZConfigRead.getProperties("fileName");// 文件名称
				dz_date_format=DZConfigRead.getProperties("dz_date_format");//对账文件时间格式
				dz_date_addday=DZConfigRead.getProperties("dz_date_addday");  
				ftp_port=DZConfigRead.getProperties("ftp_port");
				username=DZConfigRead.getProperties("ftp_username");
				password=DZConfigRead.getProperties("ftp_password");
				remotePath=DZConfigRead.getProperties("ftp_filepath");
			}

		}else{
			source_type=DZConfigRead.getProperties("source_type_"+mercharnum);
			address=DZConfigRead.getProperties("ftp_ip_"+mercharnum);
			local_path=DZConfigRead.getProperties("local_path_"+mercharnum);
			fileName=DZConfigRead.getProperties("fileName_"+mercharnum);
			dz_date_format=DZConfigRead.getProperties("dz_date_format_"+mercharnum);
			dz_date_addday=DZConfigRead.getProperties("dz_date_addday_"+mercharnum);
			ftp_port=DZConfigRead.getProperties("ftp_port_"+mercharnum);
			username=DZConfigRead.getProperties("ftp_username_"+mercharnum);
			password=DZConfigRead.getProperties("ftp_password_"+mercharnum);
			remotePath=DZConfigRead.getProperties("ftp_filepath_"+mercharnum);
		}

		SimpleDateFormat sdf = new SimpleDateFormat(dz_date_format);
		Calendar calendar = Calendar.getInstance();
		if (StringHelper.isEmpty(dzDate)) {
			calendar.add(Calendar.DAY_OF_MONTH, Integer.valueOf(dz_date_addday.toString()));
		} else {
			calendar.setTime(PowerDateFormat.getInstance(DateUtil.YYYYMMDD).parse(dzDate));
		}
		String filename = fileName.replace("{merchant}", mercharnum).replace("{date}", sdf.format(calendar.getTime()));
		File file=new File(local_path, filename);
		LOG.addDebug("连接ftp取数据:{ftp_ip:"+address+",ftp_port:"+ftp_port+",ftp_remotepath:"+remotePath+",filename:"+file.getName()+"}");
		LOG.addDebug("FTP账号信息:{username:"+username+",pwd:"+password+"}");
		if(null!=source_type && source_type.equals("1")){
			dzInfoArray= new ReadLocalDZFile().ReadFile(file, mercharnum,dzDate);
		}else if("2".equals(source_type)){
			boolean download = false;
			int port = StringHelper.isEmpty(ftp_port) ? 21 : Integer.valueOf(ftp_port);
			try {
				download = new FtpUtil(address,port,username,password,remotePath).downFile(file);
			} catch (Exception e) {
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("ftp异常");
				LOG.addError("连接ftp异常"+e);
				return dzInfoArray;
			}
			if (!download) {
				dzInfoArray.setRc("-1");
				dzInfoArray.setErrormsg("ftp上未找到对账文件"+file.getName());
				LOG.addError("ftp上未找到对账文件"+file.getName());
				return dzInfoArray;
			}
			dzInfoArray= new ReadLocalDZFile().ReadFile(file, mercharnum,dzDate);
		}else{
			dzInfoArray.setRc("-1");
			dzInfoArray.setErrormsg("未获取到实现方式获取实现方式暂未开放");
			LOG.addError("未获取到实现方式获取实现方式暂未开放");
		}

		return dzInfoArray;
	}

	public static void main(String[] args) {
		DZInfoArray  dzInfoArray=new DZInfoArray();
		dzInfoArray=	new DZSerivceImpl().getDZData("141532280620002", "20180108");
		DZInfo[]  d= dzInfoArray.getDzInfo();
		
		
		for (int i = 0; i < d.length; i++) {
			System.out.println(d[i]);
		}
	/*	int s1 = 0;
		int s2 = 0;
		int s3 = 0;
		Double s1a = Double.valueOf(0);
		Double s2a = Double.valueOf(0);
		Double s3a = Double.valueOf(0);*/

/*
		if(null!=d && d.length>0){
			System.out.println(d.length);
			for (int i = 0; i < d.length; i++) {
				String tr = d[i].getTranCode();
				System.out.println(d[i].toString());
				
				if("s1".equalsIgnoreCase(tr)){
					s1+=1;
					s1a = s1a + Double.valueOf(d[i].getAmount()) ;
				}
				if("s2".equalsIgnoreCase(tr)){
					s2+=1;
					s2a = s3a + Double.valueOf(d[i].getAmount()) ;
				}
				if("s3".equalsIgnoreCase(tr)){
					s3+=1;
					s3a = s3a + Double.valueOf(d[i].getAmount()) ;
				}
			}
		}
		System.out.println(d.length);
		System.out.println(s1+"$"+s1a);
		System.out.println(s2+"$"+s2a);
		System.out.println(s3+"$"+s3a);
*/
		
	}

}
